﻿Ext.define("PSI.BaseCode.Admin.MenuEdit", {

    createUI: function (data) {
        var me = this;
        var AddMenuWindow = Ext.create("Ext.window.Window", {
            title:data==null?"新增菜单":"编辑菜单",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 400,
            height: 160,
            border: false,
            layout: "fit",
            items: [
                 {
                     id: "editForm",
                     xtype: "form",
                     layout: "form",
                     height: "100%",
                     bodyPadding: 5,
                     defaultType: 'textfield',
                     fieldDefaults: {
                         labelWidth: 60,
                         labelAlign: "right",
                         labelSeparator: "",
                         msgTarget: 'side'
                     },
                     items: [
                           {
                               xtype: "hidden",
                               name: "id",
                               value: data == null ? null : data.id
                           },{
                               id: "title",
                               fieldLabel: "标题",
                               allowBlank: false,
                               blankText: "没有输入名称",
                               beforeLabelTextTpl: PSI.Const.REQUIRED,
                               name: "caption",
                               value: data == null ? "" : data.caption
                           }, {
                               xtype: "psi_main_admin_menufield",
                               id: "parentMenu",
                               fieldLabel: "上级菜单",
                               value: data == null ? "" : data.qtitle
                           },{
                               xtype: "hidden",
                               id: "parentids",
                               name: "ParentId",
                               value: data == null ? "" : data.ParentId
                           }, {
                               xtype: "psi_main_admin_fidfield",
                               id: "fid",
                               fieldLabel: "fid",
                               name: "fid",
                               value: data == null ? "" : data.fid
                           }
                     ],
                     buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: this.onOK,
                            scope: this
                        },
                        {
                            text: "取消", handler: function () {
                                var me = this;
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.addMenuWindow.close();
                                });
                            }, scope: this
                        }
                     ]
                 }

            ]

        }).show();

        me.addMenuWindow = AddMenuWindow;
    },

    show: function (item) {
        
        var me = this;

        return me.createUI(item);
    },

    onOK: function () {
        var me = this;
        var treestore = Ext.getCmp("menutree").getStore();

        var f = Ext.getCmp("editForm");
        var el = f.getEl();
        el.mask("数据保存中...");
        f.submit({
            url: "/MainMenuAdmin/AddorEditMenuItem",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                var cc = action.result.success;
                if (cc) {
                    PSI.MsgBox.showInfo("数据保存成功", function () {
                        me.addMenuWindow.close();
                        treestore.load();

                        //var note = treestore.getNodeById(metadataid);

                        //Ext.getCmp("caregorytree").getSelectionModel().clearSelections();

                        //Ext.getCmp("caregorytree").getSelectionModel().select(note);
                        
                    });
                }
                else {
                    Ext.Msg.alert('Failure', action.result.msg);
                }
            },
            failure: function (form, action) {
                el.unmask();
                switch (action.failureType) {
                    case Ext.form.action.Action.CLIENT_INVALID:
                        Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                        break;
                    case Ext.form.action.Action.CONNECT_FAILURE:
                        Ext.Msg.alert('Failure', 'ajax链接失败!');
                        break;
                    case Ext.form.action.Action.SERVER_INVALID:
                        Ext.Msg.alert('Failure', action.result.msg);
                }
            }
        });
    }

});